#pragma once

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <memory>
#include "Common.hpp"
#include "InetAddr.hpp"
#include "Log.hpp"

using namespace LogModule;

namespace SocketModule
{
    const int gbacklog = 8;
    const int defaultsockfd = -1;
    class Socket
    {
    public:
        virtual void SocketOrDie() = 0;
        virtual void BindOrDie(uint16_t port) = 0;
        virtual void ListenOrDie(int backlog) = 0;
        virtual std::shared_ptr<Socket> Accept(InetAddr *client) = 0;
        virtual int Recv(std::string *out) = 0;
        virtual int Send(const std::string &message) = 0;
        virtual void Close() = 0;

    public:
        void BuildTcpSocketMethod(uint16_t port, int backlog = gbacklog)
        {
            SocketOrDie();
            BindOrDie(port);
            ListenOrDie(backlog);
        }
    };

    class TcpSocket : public Socket
    {
    public:
        TcpSocket() : _sockfd(defaultsockfd)
        {
        }
        TcpSocket(int sockfd) : _sockfd(sockfd)
        {
        }
        void SocketOrDie() override
        {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                LOG(LogLevel::FATAL) << "socket error!";
                exit(SOCKET_ERR);
            }
            LOG(LogLevel::FATAL) << "socket success, sockfd: " << _sockfd;
        }

        void BindOrDie(uint16_t port) override
        {
            InetAddr addr("0", port);
            int n = ::bind(_sockfd, addr.NetAddrPtr(), addr.NetAddrLen());
            if (n < 0)
            {
                LOG(LogLevel::FATAL) << "bind error!";
                exit(BIND_ERR);
            }
            LOG(LogLevel::FATAL) << "bind success, sockfd: " << _sockfd;
        }

        void ListenOrDie(int backlog = gbacklog) override
        {
            int n = ::listen(_sockfd, backlog);
            if (n < 0)
            {
                LOG(LogLevel::FATAL) << "listen error!";
                exit(BIND_ERR);
            }
            LOG(LogLevel::FATAL) << "listern success, sockfd: " << _sockfd;
        }

        std::shared_ptr<Socket> Accept(InetAddr *client) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int newfd = ::accept(_sockfd, CONV(peer), &len);
            if (newfd < 0)
            {
                LOG(LogLevel::FATAL) << "accept warning...";
                return nullptr;
            }
            LOG(LogLevel::FATAL) << "accept success...";
            client->SetAddr(peer);
            return std::make_shared<TcpSocket>(newfd);
        }

        int Recv(std::string *out) override
        {
            char inbuffer[1024];
            int n = ::recv(_sockfd, inbuffer, sizeof(inbuffer) - 1, 0);
            if(n > 0)
            {
                inbuffer[n] = 0;
                *out += inbuffer;
            }
            return n;
        }
        int Send(const std::string &message) override
        {
            int n = ::send(_sockfd, message.c_str(), message.size(), 0);
            return n;
        }
        void Close()
        {
            if (_sockfd > 0)
                ::close(_sockfd);
        }

    private:
        int _sockfd;
    };
}